package com.yxhl.zoume.core.coupon.adapter;

import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.yxhl.zoume.HistoryCouponItemDataBinding;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.data.http.model.coupon.CouponInfo;
import com.yxhl.zoume.utils.CollectionUtils;

import java.util.List;

/**
 * Created by vhawk on 2016/11/3.
 */
public class HistoryCouponAdapter extends BaseAdapter<HistoryCouponAdapter.HistoryCouponViewHolder> {

    List<CouponInfo> historyCoupons;

    public HistoryCouponAdapter setHistoryCoupons(List<CouponInfo> historyCoupons) {
        this.historyCoupons = historyCoupons;
        notifyDataSetChanged();
        return this;
    }

    @Override
    public HistoryCouponAdapter.HistoryCouponViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        HistoryCouponItemDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_coupon_history, parent, false);
        HistoryCouponAdapter.HistoryCouponViewHolder holder = new HistoryCouponAdapter.HistoryCouponViewHolder(dataBinding);
        return holder;
    }

    @Override
    public void onBindViewHolder(HistoryCouponAdapter.HistoryCouponViewHolder holder, int position) {
        holder.bindTo(historyCoupons.get(position));
    }

    @Override
    public int getItemCount() {
        if (CollectionUtils.isEmpty(historyCoupons)){
            return 0;
        }else {
            return historyCoupons.size();
        }
    }

    public class HistoryCouponViewHolder extends BaseAdapter.BaseViewHolder {
        HistoryCouponItemDataBinding dataBinding;
        public HistoryCouponViewHolder(HistoryCouponItemDataBinding dataBinding) {
            super(dataBinding.getRoot());
            this.dataBinding = dataBinding;
        }

        public void bindTo(CouponInfo couponInfo) {
            dataBinding.setHistoryCouponInfo(couponInfo);
        }
    }
}
